package com.dsh.authorityManagement.service.impl;



import com.alibaba.fastjson.JSONObject;
import com.dsh.authorityManagement.dao.UserDao;
import com.dsh.authorityManagement.entity.dto.IdDto;
import com.dsh.authorityManagement.entity.dto.UserDto;
import com.dsh.authorityManagement.entity.vo.ImportUserVo;
import com.dsh.authorityManagement.entity.vo.UserVo;
import com.dsh.authorityManagement.service.UserService;
import com.dsh.util.MD5AndKL;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserImpl implements UserService {
    @Autowired
    UserDao userDao;
    @Override
    public int insertUser(UserDto userDto) {
        userDto.setLabel((JSONObject.toJSONString(userDto.getLabels())));
        userDto.setPassword(MD5AndKL.MD5("123456"));
        int i = userDao.insertUser(userDto);
        return i;
    }

    @Override
    public List<UserVo> selectAdminByAdminId(int id) {
        List<UserVo> userVos = userDao.selectAdminByAdminId(id);
        return userVos;
    }

    @Override
    public int insertUserForVx(UserDto userDto) {
        userDto.setLabel((JSONObject.toJSONString(userDto.getLabels())));
        userDto.setPassword(MD5AndKL.MD5("123456"));
        int i = userDao.insertUserForVx(userDto);
        return i;
    }

    @Override
    public int passed(int id) {
        int passed = userDao.passed(id);
        return passed;
    }

    @Override
    public int noPassed(int id) {
        int i = userDao.noPassed(id);
        return i;
    }

    @Override
    public PageInfo<UserDto> selectUsernameAndRoleNameAndStoreName(String username,String phone,int limit, int page) {
        PageHelper.startPage(page,limit);
        if ((username == null) && (phone == null)){
            List<UserDto> users = userDao.selectUsernameAndRoleNameAndStoreName();
            return new PageInfo<>(users);
        }
        else {
            List<UserDto> usernameAndRoleNameAndStoreNameM= userDao.selectUsernameAndRoleNameAndStoreNameM(username,phone);
            return new PageInfo<>(usernameAndRoleNameAndStoreNameM);
        }

    }

    @Override
    public List<ImportUserVo> selectImportUser() {
        List<ImportUserVo> importUserVos = userDao.selectImportUser();
        return importUserVos;
    }

    @Override
    public void ImportUser(Integer[] ids, int sId) {
        for (Integer id : ids) {
            IdDto idDto = new IdDto();
            idDto.setsId(sId);
            idDto.setuId(id);
            this.userDao.ImportUser(idDto);
        }

    }
}
